package com.ymatou.c2cliveindexer.testcase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.c2cliveindexer.parameter.C2cLiveBean;
import com.ymatou.c2cliveindexer.service.C2cLiveCall;
import com.ymatou.c2cliveindexer.service.C2cSearchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sqlwapper.AppDefaultWapper;
import com.ymttest.database.sqlwapper.DefaultWapper;
/**
*将数据建立索引置于索引区接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_C2cLive{
	private static	C2cLiveBean c2cliveindexerBean;
	private static  C2cLiveCall c2cliveindexerCall;
	private static  C2cSearchCall c2cLiveIndexBizerCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("将数据建立索引置于索引区接口");
	}

	@Before
	public void caseUp() {
		c2cliveindexerBean=new C2cLiveBean();
		c2cliveindexerCall=new C2cLiveCall();
		c2cLiveIndexBizerCall = new C2cSearchCall();
		c2cliveindexerBean.setAction("reload");
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(c2cliveindexerCall.getOpurl());
		
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_C2cLive_001() {
		Logger.start(true,"全量返回ok");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			
			c2cliveindexerCall.setData(c2cliveindexerBean);
			c2cliveindexerCall.callService();			
			Logger.comment("c2cliveindexerCall send:" + c2cliveindexerCall.getEntityString());
			Logger.comment("c2cliveindexerCall response:" + c2cliveindexerCall.getReturnData());
			Logger.verifyEquals("OK",c2cliveindexerCall.getStatus(), "全量更新状态是否成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_C2cLive_002() {
		Logger.start(true,"未经认证的买手直播数据不会进入索引");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			Map m = new HashMap();
			// 1:北美，2：欧洲，3：亚洲，4：澳新
			m.put("CountryGroupId", 0);
			m.put("LiveActivityId", 0);
			c2cliveindexerCall.setData(c2cliveindexerBean);
			c2cliveindexerCall.callService();	
			
			//访问索引文件
			c2cLiveIndexBizerCall.setUrl("http://es.ymatou.com:19200/c2clive/_search");
			c2cLiveIndexBizerCall.callService();	
			//获取索引文件中的总记录数
			Integer indexTotal = (Integer) c2cLiveIndexBizerCall.getData().get("total");
			//从数据库获取同步直播数据
			List<Map> LiveActivityList = new  AppDefaultWapper().getsLiveActivity(m);
			final int liveSize = LiveActivityList.size();
			//将数据库中所有直播sellid放到新数组liveAry中
			int[] liveAry = new int[liveSize];
			//将还没有过滤掉未认证的买手信息存放到数组liveAry中
			for(int i=0; i<liveSize;i++){
				liveAry[i] = (Integer) LiveActivityList.get(i).get("sellerId");
			}
			//获取所有认证买手id
			List<Map> sellList = new DefaultWapper().selectLicenceSeller();
			final int size = sellList.size();
			//将所有认证过的买手id存放到数组ary中
			int[] ary = new int[size];
			for (int i = 0; i < size; i++) {
				ary[i] = (Integer) sellList.get(i).get("sellerId");
				
			}
			//处理liveAry和ary数组，返回两个数组的交集即为合格直播数据
			
			LinkedList<Integer> list=new LinkedList<Integer>();
			  for(int i=0;i<size;i++)
			   for(int j=0;j<liveSize;j++)
			    if(ary[i]==liveAry[j]){
			     list.add(ary[i]);
			    }
			
			Logger.comment("c2cliveindexerCall send:" + c2cliveindexerCall.getEntityString());
			Logger.comment("c2cliveindexerCall response:" + c2cliveindexerCall.getReturnData());
			Logger.verifyEquals(indexTotal,list.size(), "创建索引数据总数正确");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_C2cLive_003() {
		Logger.start(true,"索引文件更新情况");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			
			c2cliveindexerCall.setData(c2cliveindexerBean);
			c2cliveindexerCall.callService();	
			//取出索引文件中的数据
			c2cLiveIndexBizerCall.setUrl("http://es.ymatou.com:19200/c2clive/_search");
			c2cLiveIndexBizerCall.callService();	
			//取索引文件第一条记录得到更新时间
			JSONArray DataOne =  ((JSONArray) c2cLiveIndexBizerCall.getData().get("hits"));
			JSONObject one = (JSONObject) DataOne.get(0);
			//得到索引文件中更新的时间,如c2clive20160129190856
			String UpdateTime = (String) one.get("_index");
			//截取更新字符串中的yyyyMMddHH
			String firleTime = UpdateTime.substring(7, 17);
			//获取当前时间精确到YYYY-MM-dd HH
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHH");//设置日期格式
			// new Date()为获取当前系统时间
			String nowData = df.format(new Date());
			
			Logger.comment("c2cliveindexerCall send:" + c2cliveindexerCall.getEntityString());
			Logger.comment("c2cliveindexerCall response:" + c2cliveindexerCall.getReturnData());
			//对比两个更新时间
			Logger.verifyEquals(nowData,firleTime, "索引文件已成功更新");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}